
package com.jzdy.controller;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.jzdy.R;
import com.jzdy.core.controller.CoreFragment;
import com.jzdy.core.utils.CoreActivityHelper;
import com.special.ResideMenu.ResideMenu;

public class HomeFragment extends CoreFragment implements OnClickListener
{
    private ViewPager mViewPager;
    private HomePagerAdapter mHomePagerAdapter;
    protected MainActivity mainActivity;
    private RadioGroup mRadioGroup;

    @Override
    public int doGetContentViewId()
    {
        // TODO Auto-generated method stub
        return R.layout.home;
    }

    @Override
    public void doInitSubViews(View containerView)
    {
        // TODO Auto-generated method stub
        mViewPager = (ViewPager) containerView.findViewById(R.id.viewpager);
        containerView.findViewById(R.id.iv_slide).setOnClickListener(this);
        containerView.findViewById(R.id.iv_add).setOnClickListener(this);
        mRadioGroup = (RadioGroup) containerView.findViewById(R.id.radiogroup);
        ((RadioButton) mRadioGroup.getChildAt(0)).setChecked(true);
        mRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(RadioGroup group , int checkedId)
            {
                // TODO Auto-generated method stub
                int index = mRadioGroup.indexOfChild(mRadioGroup.findViewById(checkedId));
                if (mViewPager.getCurrentItem() != index)
                {
                    mViewPager.setCurrentItem(index, true);
                }
            }
        });
    }

    @Override
    public void doInitDataes()
    {
        // TODO Auto-generated method stub
        mainActivity = (MainActivity) getActivity();
        mainActivity.getResideMenu().setSwipeDirectionDisable(ResideMenu.DIRECTION_RIGHT);
        mHomePagerAdapter = new HomePagerAdapter(getFragmentManager());
        mViewPager.setAdapter(mHomePagerAdapter);
        mViewPager.setOnPageChangeListener(new OnPageChangeListener()
        {
            @Override
            public void onPageSelected(int arg0)
            {
                // TODO Auto-generated method stub
                mainActivity.getResideMenu().getDisabledSwipeDirection().clear();
                mainActivity.getResideMenu().setSwipeDirectionDisable(arg0 == 0 ? ResideMenu.DIRECTION_RIGHT : ResideMenu.DIRECTION_LEFT);
                mainActivity.getResideMenu().setSwipeDirectionDisable(arg0 == 1 ? ResideMenu.DIRECTION_LEFT : ResideMenu.DIRECTION_RIGHT);
                RadioButton radioButton = (RadioButton) mRadioGroup.getChildAt(arg0);
                if (!radioButton.isChecked())
                {
                    radioButton.setChecked(true);
                }
            }

            @Override
            public void onPageScrolled(int arg0 , float arg1 , int arg2)
            {
                // TODO Auto-generated method stub
            }

            @Override
            public void onPageScrollStateChanged(int arg0)
            {
                // TODO Auto-generated method stub
            }
        });
    }

    @Override
    public void doAfter()
    {
        // TODO Auto-generated method stub
    }

    class HomePagerAdapter extends FragmentPagerAdapter
    {
        public HomePagerAdapter(FragmentManager fm)
        {
            super(fm);
            // TODO Auto-generated constructor stub
        }

        @Override
        public Fragment getItem(int arg0)
        {
            // TODO Auto-generated method stub
            CoreFragment mCoreFragment = null;
            if (arg0 == 0)
            {
                mCoreFragment = new DingYueFragment();
            }
            else if (arg0 == 1)
            {
                mCoreFragment = new FaXianFragment();
            }
            else
            {
                mCoreFragment = new DingYueFragment();
            }
            return mCoreFragment;
        }

        @Override
        public int getCount()
        {
            // TODO Auto-generated method stub
            return 2;
        }
    }

    @Override
    public void onClick(View v)
    {
        // TODO Auto-generated method stub
        switch (v.getId())
        {
            case R.id.iv_slide:
                mainActivity.getResideMenu().toggleMenu();
                break;
            case R.id.iv_add:
                CoreActivityHelper.startActivity(mActivity, AddChannelActivity.class);
                break;
            default:
                break;
        }
    }

    public ViewPager getmViewPager()
    {
        return mViewPager;
    }
}
